*-------------------------------------------------------------------------------
* Objective: Generate metals and fuels Data
*-------------------------------------------------------------------------------

********************************************************************************
**# Step 1: Load and clean raw datasets
/*******************************************************************************

Ores and Metals (% of Merchandise Exports)
Source: World Bank WDI (Beta)
Notes: We take values for all countries, in all months and years versions of the variable, then take averages.
       We take 0s as missing values
	   Data accessed 12.17.2020
	   
Ores and Metals (% of Merchandise Exports)
Source: World Bank WDI (NO BETA)
Notes: We take 0s as missing values
       Data accessed 12.17.2020
	   
Fuels (% of Merchandise Exports)
Source: World Bank WDI (Beta)
Notes: We take values for all countries, in all months and years versions of the variable, then take averages.
       We take 0s as missing values
  	   Data accessed 12.20.2020

Fuels (% of Merchandise Exports)
Source: World Bank WDI (NO BETA)
Notes: We take 0s as missing values
       Data accessed 12.17.2020
	   
*******************************************************************************/

* Importing data 
clear all
import excel "raw_datasets/oresmetals_x_b.xlsx", sheet("Data") firstrow clear
			
* Genereting missing values
quietly{
	foreach var of varlist y1960-y2019{
	replace `var' = "" if `var' == ".." | `var' == "0"
	}
destring y*, replace
}
	
* Matching variable name of country code with the one used in the main dataset
rename CountryCode code_wb

* Taking average over all observations of each year
collapse y*, by(code_wb)

* Generating year 2020, dropping 2019
rename y2019 y2020

* Reshaping into long shape and renaming variable
reshape long y, i(code_wb) j(year)
rename y oresmetals_x_b

* Keeping only desired countries
merge m:1 code_wb using "processed_datasets/ccode", keepusing(country_wb)
keep if _merge == 3
drop _merge country_wb

* Saving
save "processed_datasets/dataset_metfuel", replace

*-------------------------------------------------------------------------------
* Merging the rest of the data

foreach dmetfuel in oresmetals_x fuel_x_b fuel_x{
    
	* Importing data
	clear all
	import excel "raw_datasets/`dmetfuel'.xlsx", sheet("Data") firstrow clear
	
	* Dropping 2020
	capture drop y2020
		
	* Genereting missing values
	quietly{
		foreach var of varlist y1960-y2019{
		replace `var' = "" if `var' == ".." | `var' == "0"
		}
	destring y*, replace
	}
	
	* Matching variable name of country code with the one used in the main dataset
	rename CountryCode code_wb

	* Taking average over all observations of each year
	collapse y*, by(code_wb)

	* Generating year 2020, dropping 2019
	rename y2019 y2020

	* Reshaping into long shape and renaming variable
	reshape long y, i(code_wb) j(year)
	rename y `dmetfuel'
	
	* Saving
	tempfile `dmetfuel'_data
	save ``dmetfuel'_data', replace
	
	* Merging to main dataset
	use "processed_datasets/dataset_metfuel", clear
	merge 1:1 code_wb year using ``dmetfuel'_data'
	drop if _merge == 2
	drop _merge
	save "processed_datasets/dataset_metfuel", replace 
	
}

********************************************************************************
**# Step 2.1: Missing Data - General Approach
/*******************************************************************************
First we use the beta version of the datasets whenever they can replace a
missing observation. Then we approximate 2020 with the closest non-missing year
since 2016. Then we apply linear interpolation to replace missing values
*******************************************************************************/

foreach oresfuel in oresmetals fuel{
	
	* Combining WDI and WDI Beta
	gen `oresfuel'_x_comb = `oresfuel'_x
	replace `oresfuel'_x_comb = `oresfuel'_x_b if `oresfuel'_x_comb == . & `oresfuel'_x_b != .
	replace `oresfuel'_x_comb = 0 if `oresfuel'_x_comb < 0

	* Approximating 2020 with previous values
	gen identi = 0
	sort code_wb year
	by code_wb: replace identi = 1 if `oresfuel'_x_comb != . & `oresfuel'_x_comb[60] == . & year == 2018
	by code_wb: replace `oresfuel'_x_comb = `oresfuel'_x_comb[59] if `oresfuel'_x_comb == . & year == 2020
	by code_wb: replace identi = 1 if `oresfuel'_x_comb != . & `oresfuel'_x_comb[60] == . & year == 2017
	by code_wb: replace `oresfuel'_x_comb = `oresfuel'_x_comb[58] if `oresfuel'_x_comb == . & year == 2020
	by code_wb: replace identi = 1 if `oresfuel'_x_comb != . & `oresfuel'_x_comb[60] == . & year == 2016
	by code_wb: replace `oresfuel'_x_comb = `oresfuel'_x_comb[57] if `oresfuel'_x_comb == . & year == 2020
	replace `oresfuel'_x_comb = . if identi == 1
	drop identi

	* Filling missing values with interpolation
	sort code_wb year
	by code_wb: ipolate `oresfuel'_x_comb year, gen(intores)
	replace `oresfuel'_x_comb = intores if `oresfuel'_x_comb == .
	drop intores

}

*-------------------------------------------------------------------------------
* We modify Singapore fuel exports to 0, as the country does not have oil reserves, all it's exports are the result of importing and refining.
replace fuel_x_comb = 0 if code_wb == "SGP"

********************************************************************************
* Generate the combined minfuel variable (minerals + fuel exports)
********************************************************************************

egen minfuel_x = rowtotal(oresmetals_x_comb fuel_x_comb)
replace minfuel_x = . if minfuel_x == 0

********************************************************************************
**# Step 2.2: Missing Data - Fixing missing values country by country
/*******************************************************************************
WDI data can sometimes omit exports we are interested in accounting. To fix this
and in adittion fill some of the data still missing we use several sources:

- USGS
- WTO Data: "Fuels and mining products" on "Total merchandise trade"
- Trade Map (ITC) (Converted using the UNSTATS conversion table from HS (2012) 
  to SITC (Rev. 3))
*******************************************************************************/

* Bangladesh - Source: WTO Data (https://data.wto.org/) - Data is estimated
* Notes: USGS data not available

replace minfuel_x = (125/39337)*100 if year == 2020 & code_wb == "BGD"
replace minfuel_x = (157/39252)*100 if year == 2018 & code_wb == "BGD"
replace minfuel_x = (180/35851)*100 if year == 2017 & code_wb == "BGD"
replace minfuel_x = (139/34894)*100 if year == 2016 & code_wb == "BGD"

*-------------------------------------------------------------------------------

* Bhutan - Source: WTO Data (https://data.wto.org/) - Data is estimated
* Notes: We take 2015 as 2020 and interpolate later
*        USGS data not available
*        ITC data not compatible with series

replace minfuel_x = (180/583)*100 if year == 2014 & code_wb == "BTN"
replace minfuel_x = (240/544)*100 if year == 2013 & code_wb == "BTN"

replace minfuel_x = (223/549)*100 if year == 2020 & code_wb == "BTN"

*-------------------------------------------------------------------------------

* Chad - Source Trade Map (ITC) - Mirror Data * Data not available in USGS

replace minfuel_x = 95.2 if year == 2020 & code_wb == "TCD"
replace minfuel_x = 95.8 if year == 2018 & code_wb == "TCD"
replace minfuel_x = 93.5 if year == 2017 & code_wb == "TCD"
replace minfuel_x = 93.5 if year == 2016 & code_wb == "TCD"
replace minfuel_x = 93.9 if year == 2015 & code_wb == "TCD"
replace minfuel_x = 97.2 if year == 2014 & code_wb == "TCD"
replace minfuel_x = 96.1 if year == 2013 & code_wb == "TCD"
replace minfuel_x = 96.9 if year == 2012 & code_wb == "TCD"
replace minfuel_x = 97.6 if year == 2011 & code_wb == "TCD"
replace minfuel_x = 88.7 if year == 2010 & code_wb == "TCD"
replace minfuel_x = 95.9 if year == 2009 & code_wb == "TCD"
replace minfuel_x = 97.5 if year == 2008 & code_wb == "TCD"

*-------------------------------------------------------------------------------

* Cuba - Source: USGS

* We take 2015 as 2020

replace minfuel_x = 15 if year == 2020 & code_wb == "CUB"
replace minfuel_x = . if year == 2019 & code_wb == "CUB"
replace minfuel_x = . if year == 2018 & code_wb == "CUB"
replace minfuel_x = . if year == 2017 & code_wb == "CUB"
replace minfuel_x = . if year == 2016 & code_wb == "CUB"
replace minfuel_x = . if year == 2015 & code_wb == "CUB"
replace minfuel_x = 15 if year == 2014 & code_wb == "CUB"
replace minfuel_x = 13.6 if year == 2013 & code_wb == "CUB"
replace minfuel_x = . if year == 2012 & code_wb == "CUB"
replace minfuel_x = 23 if year == 2011 & code_wb == "CUB"
replace minfuel_x = . if year == 2010 & code_wb == "CUB"
replace minfuel_x = 29 if year == 2009 & code_wb == "CUB"
replace minfuel_x = 50 if year == 2008 & code_wb == "CUB"

*-------------------------------------------------------------------------------

* Djibouti - We can't find information about mining/oil being relevant for the economy of Djibouti. Hence we set exports = 0

replace minfuel_x = 0 if year == 2020 & code_wb == "DJI"

*-------------------------------------------------------------------------------

* DRC - Source: USGS for years < 2015. Complemented with Trade Map (ITC) (Mirror data) after 2014
* Notes: We use 2019 as 2020
* Notes: We also include HS 7106 (Diamonds) for ITC data

replace minfuel_x = 95.9 if year == 2020 & code_wb == "COD"
replace minfuel_x = 85.7 if year == 2018 & code_wb == "COD"
replace minfuel_x = 89.3 if year == 2017 & code_wb == "COD"
replace minfuel_x = 89.83 if year == 2016 & code_wb == "COD"
replace minfuel_x = 94.14 if year == 2015 & code_wb == "COD"
replace minfuel_x = 95 if year == 2014 & code_wb == "COD"
replace minfuel_x = 96 if year == 2013 & code_wb == "COD"
replace minfuel_x = 97 if year == 2011 & code_wb == "COD"
replace minfuel_x = 96 if year == 2010 & code_wb == "COD"
replace minfuel_x = 96 if year == 2009 & code_wb == "COD"
replace minfuel_x = 96 if year == 2008 & code_wb == "COD"
 
*-------------------------------------------------------------------------------

* Equatorial Guinea - Source: USGS (2014 & 2015), rest is complemented with Trade Map (ITC) (Mirror data)
* Notes: Export shares of just Hydrocarbons - 2019 is taken as 2020

replace minfuel_x = 89.06 if year == 2020 & code_wb == "GNQ"
replace minfuel_x = 87.09 if year == 2018 & code_wb == "GNQ"
replace minfuel_x = 86.28 if year == 2017 & code_wb == "GNQ"
replace minfuel_x = 87.72 if year == 2016 & code_wb == "GNQ"
replace minfuel_x = 88 if year == 2015 & code_wb == "GNQ"
replace minfuel_x = 92 if year == 2014 & code_wb == "GNQ"
replace minfuel_x = 93.39 if year == 2013 & code_wb == "GNQ"
replace minfuel_x = 94.61 if year == 2012 & code_wb == "GNQ"
replace minfuel_x = 92.42 if year == 2011 & code_wb == "GNQ"
replace minfuel_x = 92.34 if year == 2010 & code_wb == "GNQ"
replace minfuel_x = 93.49 if year == 2009 & code_wb == "GNQ"
replace minfuel_x = 96.49 if year == 2008 & code_wb == "GNQ"

*-------------------------------------------------------------------------------

* Eritrea - Source: USGS (2012 and 2011) - Complemented with ITC mirror data.
* Notes: We take 2019 as 2020 - We include HS 7108 (Gold, incl. gold plated with platinum, unwrought or not further worked than semi-manufactured or in powder form)

replace minfuel_x = 98.9 if year == 2020 & code_wb == "ERI"
replace minfuel_x = 98.6 if year == 2018 & code_wb == "ERI"
replace minfuel_x = 97.5 if year == 2017 & code_wb == "ERI"
replace minfuel_x = 96.1 if year == 2016 & code_wb == "ERI"
replace minfuel_x = 97.1 if year == 2015 & code_wb == "ERI"
replace minfuel_x = 91.2 if year == 2014 & code_wb == "ERI"
replace minfuel_x = 91.2 if year == 2013 & code_wb == "ERI"
replace minfuel_x = 89 if year == 2012 & code_wb == "ERI"
replace minfuel_x = 95 if year == 2011 & code_wb == "ERI"
replace minfuel_x = 0 if year == 2010 & code_wb == "ERI"
replace minfuel_x = 0.1 if year == 2009 & code_wb == "ERI"
replace minfuel_x = 0.1 if year == 2008 & code_wb == "ERI"

*-------------------------------------------------------------------------------

* Gabon 

* Source: USGS for 2015 and 2014, complemented with WTO Data (https://data.wto.org/) (Data is estimated) for the rest of the years.
* Notes: We use 2018 as 2020
* Notes: ITC data very similar to WTO

replace minfuel_x = (5916/6687)*100 if year == 2020 & code_wb == "GAB"
replace minfuel_x = (4581/5165)*100 if year == 2017 & code_wb == "GAB"
replace minfuel_x = (3375/4345)*100 if year == 2016 & code_wb == "GAB"
replace minfuel_x = 88 if year == 2015 & code_wb == "GAB"
replace minfuel_x = 94 if year == 2014 & code_wb == "GAB"
replace minfuel_x = (8229/10039)*100 if year == 2013 & code_wb == "GAB"
replace minfuel_x = (8989/9493)*100 if year == 2012 & code_wb == "GAB"
replace minfuel_x = (8833/9766)*100 if year == 2011 & code_wb == "GAB"
replace minfuel_x = (7342/8686)*100 if year == 2010 & code_wb == "GAB"

*-------------------------------------------------------------------------------

* Guinea-Bissau - Can't find evidence that mineral production was relevant for Guinea-Bissau so set exports = 0

replace minfuel_x = 0 if year == 2020 & code_wb == "GNB"

*-------------------------------------------------------------------------------

* Haiti - Source: WTO Data (https://data.wto.org/) - Data is estimated between 2015 and 2019
* We take 2019 as 2020

replace minfuel_x = (21/1200)*100 if year == 2020 & code_wb == "HTI"
replace minfuel_x = (12/1078)*100 if year == 2018 & code_wb == "HTI"
replace minfuel_x = (17/980)*100 if year == 2017 & code_wb == "HTI"

foreach haitiyear of numlist 1998/2014{
	replace minfuel_x = 0 if year == `haitiyear' & code_wb == "HTI"
} 

*-------------------------------------------------------------------------------

* Liberia - Source: USGS
* Notes: Data for iron ore, gold and diamonds - We use 2015 as 2020

replace minfuel_x = 53.9 if year == 2020 & code_wb == "LBR"
replace minfuel_x = 73 if year == 2014 & code_wb == "LBR"
replace minfuel_x = 34.3 if year == 2012 & code_wb == "LBR"
replace minfuel_x = 12.6 if year == 2011 & code_wb == "LBR"
replace minfuel_x = 17.2 if year == 2010 & code_wb == "LBR"
replace minfuel_x = 13.2 if year == 2009 & code_wb == "LBR"
replace minfuel_x = 13.2 if year == 2008 & code_wb == "LBR"

*-------------------------------------------------------------------------------

* Mauritania - Source: USGS
* Notes: Used to complement data post 2015 due to lack of oil data from World Bank
*        We use 2015 as 2020

replace minfuel_x = . if year > 2014 & code_wb == "MRT"
replace minfuel_x = 78.2 if year == 2020 & code_wb == "MRT"
replace minfuel_x = 78.3 if year == 2014 & code_wb == "MRT"
replace minfuel_x = 86 if year == 2013 & code_wb == "MRT"
replace minfuel_x = 78.9 if year == 2012 & code_wb == "MRT"
replace minfuel_x = 84.9 if year == 2011 & code_wb == "MRT"
replace minfuel_x = 88.6 if year == 2010 & code_wb == "MRT"
replace minfuel_x = 79.7 if year == 2009 & code_wb == "MRT"
replace minfuel_x = 81 if year == 2008 & code_wb == "MRT"
replace minfuel_x = 78 if year == 2007 & code_wb == "MRT"

*-------------------------------------------------------------------------------

* Papua New Guinea - Source: USGS - Complemented by Trade Map (ITC) (Mirror data) post 2014
* Notes: We take 2019 as 2020 - We include HS 7108 (Gold, incl. gold plated with platinum, unwrought or not further worked than semi-manufactured or in powder form)
* 2011 and 2012 are not mirror data

replace minfuel_x = 82.1 if year == 2020 & code_wb == "PNG"
replace minfuel_x = 77.5 if year == 2018 & code_wb == "PNG"
replace minfuel_x = 77.4 if year == 2017 & code_wb == "PNG"
replace minfuel_x = 75.4 if year == 2016 & code_wb == "PNG"
replace minfuel_x = 77.8 if year == 2015 & code_wb == "PNG"
replace minfuel_x = 81 if year == 2014 & code_wb == "PNG"
replace minfuel_x = 73.5 if year == 2013 & code_wb == "PNG"
replace minfuel_x = 74.3 if year == 2012 & code_wb == "PNG"
replace minfuel_x = 71.6 if year == 2011 & code_wb == "PNG"
replace minfuel_x = 78 if year == 2010 & code_wb == "PNG"
replace minfuel_x = 76 if year == 2009 & code_wb == "PNG"
replace minfuel_x = 80 if year == 2008 & code_wb == "PNG"

*-------------------------------------------------------------------------------

* Somalia
* From USGS: Minerals only made a small contribution to Somalia's total exports

replace minfuel_x = 0 if year == 2020 & code_wb == "SOM"

*-------------------------------------------------------------------------------

* Sudan - Source: Trade Map (ITC) (Mirror data)
* Notes: We take 2019 as 2020 - We include HS 7108 (Gold, incl. gold plated with platinum, unwrought or not further worked than semi-manufactured or in powder form)
* We take value of exports for Sudan and South Sudan and combine them post 2011.
* ITC data for Sudan pre-2016 shows very high values for fuel exports, could be including South Sudan, so we ignore those years.

replace minfuel_x = 63.7 if year == 2020 & code_wb == "SDN"
replace minfuel_x = 57.2 if year == 2018 & code_wb == "SDN"
replace minfuel_x = 60.9 if year == 2017 & code_wb == "SDN"
drop if code_wb == "SSD"

*-------------------------------------------------------------------------------

* Taiwan

* Source: We assign 0 to Taiwan mineral extracting industries share in exports, which is consistent with RJV2016 data

replace minfuel_x = 0 if code_wb == "TWN"

*-------------------------------------------------------------------------------

* Trinidad and Tobago - Source: USGS
* Notes: We use 2015 as 2020. We also replace one year prior to 2015

replace minfuel_x = . if year > 2014 & code_wb == "TTO"
replace minfuel_x = 78 if year == 2020 & code_wb == "TTO"
replace minfuel_x = 83 if year == 2014 & code_wb == "TTO"
replace minfuel_x = 37 if year == 2013 & code_wb == "TTO"
replace minfuel_x = . if year == 2012 & code_wb == "TTO"
replace minfuel_x = . if year == 2011 & code_wb == "TTO"
replace minfuel_x = 84 if year == 2010 & code_wb == "TTO"
replace minfuel_x = . if year == 2009 & code_wb == "TTO"
replace minfuel_x = 89 if year == 2008 & code_wb == "TTO"

*-------------------------------------------------------------------------------

* Venezuela - Source: WTO Data (https://data.wto.org/) - Data is estimated
* Notes: We use 2019 as 2020

replace minfuel_x = (16137/17185)*100 if year == 2020 & code_wb == "VEN"
replace minfuel_x = (32510/34440)*100 if year == 2018 & code_wb == "VEN"
replace minfuel_x = (30644/31960)*100 if year == 2017 & code_wb == "VEN"
replace minfuel_x = (25925/26696)*100 if year == 2016 & code_wb == "VEN"
replace minfuel_x = (35648/37309)*100 if year == 2015 & code_wb == "VEN"
replace minfuel_x = (72202/74714)*100 if year == 2014 & code_wb == "VEN"

*-------------------------------------------------------------------------------
* Syria - Based on reports from EIA (https://www.eia.gov/international/analysis/country/SYR)
*         it seems like Syrian oil fell dramatically after the conflict started (around 90%).
*         Based on this, we assign it a value of 0 in 2020.

replace minfuel_x = 0 if year == 2020 & code_wb == "SYR"

*-------------------------------------------------------------------------------
* Burkina Faso

* Problem: World Bank Data seems to be missing the gold rush post-2008

* Source: USGS (2012-2015) complemented with ITC
* Notes: We use 2019 as 2020

replace minfuel_x = 74.4 if year == 2020 & code_wb == "BFA"
replace minfuel_x = 71.4 if year == 2018 & code_wb == "BFA"
replace minfuel_x = 70 if year == 2017 & code_wb == "BFA"
replace minfuel_x = 65 if year == 2016 & code_wb == "BFA"
replace minfuel_x = 63 if year == 2015 & code_wb == "BFA"
replace minfuel_x = 66 if year == 2014 & code_wb == "BFA"
replace minfuel_x = 71 if year == 2013 & code_wb == "BFA"
replace minfuel_x = 76 if year == 2012 & code_wb == "BFA"
replace minfuel_x = 77.43 if year == 2011 & code_wb == "BFA"

*-------------------------------------------------------------------------------
* Botswana

* Problem: World Bank Data seems to be missing diamond exports

* Source: USGS (yeaers 2008,2011, and 2013-15) complemented with ITC
* Notes: We use 2019 as 2020

replace minfuel_x = 91.43 if year == 2020 & code_wb == "BWA"
replace minfuel_x = 90.2 if year == 2018 & code_wb == "BWA"
replace minfuel_x = 91.26 if year == 2017 & code_wb == "BWA"
replace minfuel_x = 88.66 if year == 2016 & code_wb == "BWA"
replace minfuel_x = 90.2 if year == 2015 & code_wb == "BWA"
replace minfuel_x = 91.2 if year == 2014 & code_wb == "BWA"
replace minfuel_x = 90.2 if year == 2013 & code_wb == "BWA"
replace minfuel_x = 79.81 if year == 2012 & code_wb == "BWA"
replace minfuel_x = 75 if year == 2011 & code_wb == "BWA"
replace minfuel_x = . if year == 2010 & code_wb == "BWA"
replace minfuel_x = . if year == 2009 & code_wb == "BWA"
replace minfuel_x = 75 if year == 2008 & code_wb == "BWA"

*-------------------------------------------------------------------------------
* Republic of Congo

* Fixing values of World Bank of Oil with USGS Data (<2016) and ITC (2018 and 2019)
* Notes: - We use 2019 as 2020
*        - The last value of USGS (2015) seems like an outlier in the series. We use the latest data of ITC as 2020.

replace minfuel_x = 83.94 if year == 2020 & code_wb == "COG"
replace minfuel_x = 82.22 if year == 2018 & code_wb == "COG"
replace minfuel_x = . if year == 2017 & code_wb == "COG"
replace minfuel_x = . if year == 2016 & code_wb == "COG"
replace minfuel_x = 73 if year == 2015 & code_wb == "COG"
replace minfuel_x = 81 if year == 2014 & code_wb == "COG"
replace minfuel_x = . if year == 2013 & code_wb == "COG"
replace minfuel_x = . if year == 2012 & code_wb == "COG"
replace minfuel_x = . if year == 2011 & code_wb == "COG"
replace minfuel_x = 89 if year == 2010 & code_wb == "COG"
replace minfuel_x = 93 if year == 2009 & code_wb == "COG"
replace minfuel_x = 94 if year == 2008 & code_wb == "COG"

*-------------------------------------------------------------------------------
* Ethiopia - Source: USGS
* Notes: - We use 2015 as 2020 and 2002 as 2000

replace minfuel_x = 10.5 if year == 2020 & code_wb == "ETH"
replace minfuel_x = . if year == 2018 & code_wb == "ETH"
replace minfuel_x = . if year == 2017 & code_wb == "ETH"
replace minfuel_x = . if year == 2016 & code_wb == "ETH"
replace minfuel_x = . if year == 2015 & code_wb == "ETH"
replace minfuel_x = 14 if year == 2014 & code_wb == "ETH"
replace minfuel_x = 19 if year == 2013 & code_wb == "ETH"
replace minfuel_x = 19 if year == 2012 & code_wb == "ETH"
replace minfuel_x = 17 if year == 2011 & code_wb == "ETH"
replace minfuel_x = 14 if year == 2010 & code_wb == "ETH"
replace minfuel_x = . if year == 2009 & code_wb == "ETH"
replace minfuel_x = . if year == 2008 & code_wb == "ETH"
replace minfuel_x = . if year == 2007 & code_wb == "ETH"
replace minfuel_x = 6.5 if year == 2006 & code_wb == "ETH"
replace minfuel_x = . if year == 2005 & code_wb == "ETH"
replace minfuel_x = . if year == 2004 & code_wb == "ETH"
replace minfuel_x = 9 if year == 2003 & code_wb == "ETH"
replace minfuel_x = . if year == 2002 & code_wb == "ETH"
replace minfuel_x = . if year == 2001 & code_wb == "ETH"
replace minfuel_x = 8 if year == 2000 & code_wb == "ETH"


*-------------------------------------------------------------------------------
* Ghana

* Problem: World Bank Data seems to be missing gold exports

* Source: USGS (yeaers 2012-15) (only Gold and Petroleoum) complemented with ITC
* Notes: We use 2019 as 2020

replace minfuel_x = 71 if year == 2020 & code_wb == "GHA"
replace minfuel_x = 68.2 if year == 2018 & code_wb == "GHA"
replace minfuel_x = 67.5 if year == 2017 & code_wb == "GHA"
replace minfuel_x = 55.9 if year == 2016 & code_wb == "GHA"
replace minfuel_x = 50 if year == 2015 & code_wb == "GHA"
replace minfuel_x = 61 if year == 2014 & code_wb == "GHA"
replace minfuel_x = 64 if year == 2013 & code_wb == "GHA"
replace minfuel_x = 64 if year == 2012 & code_wb == "GHA"
replace minfuel_x = 67.8 if  year == 2011 & code_wb == "GHA"
replace minfuel_x = 66.2 if year == 2010 & code_wb == "GHA"
replace minfuel_x = 61 if year == 2009 & code_wb == "GHA"
replace minfuel_x = 48.6 if year == 2008 & code_wb == "GHA"

*-------------------------------------------------------------------------------
* Guinea

* Problem: World Bank Data seems to be wrong

* Source: USGS (yeaers 2011-14 and 2009) complemented with ITC
* Notes: We use 2019 as 2020

replace minfuel_x = 94.7 if year == 2020 & code_wb == "GIN"
replace minfuel_x = 93.9 if year == 2018 & code_wb == "GIN"
replace minfuel_x = 92 if year == 2017 & code_wb == "GIN"
replace minfuel_x = 91.4 if year == 2016 & code_wb == "GIN"
replace minfuel_x = 82.6 if year == 2015 & code_wb == "GIN"
replace minfuel_x = 88 if year == 2014 & code_wb == "GIN"
replace minfuel_x = 96 if year == 2013 & code_wb == "GIN"
replace minfuel_x = 90 if year == 2012 & code_wb == "GIN"
replace minfuel_x = 90 if  year == 2011 & code_wb == "GIN"
replace minfuel_x = 70.1 if year == 2010 & code_wb == "GIN"
replace minfuel_x = 72 if year == 2009 & code_wb == "GIN"
replace minfuel_x = 73.7 if year == 2008 & code_wb == "GIN"

*-------------------------------------------------------------------------------
* Guyana

* Problem: World Bank Data seems to be wrong

* Source: USGS (2012-15) complemented with ITC
* Notes: We use 2018 as 2020

replace minfuel_x = 53.4 if year == 2020 & code_wb == "GUY"
replace minfuel_x = . if year == 2018 & code_wb == "GUY"
replace minfuel_x = 45 if year == 2017 & code_wb == "GUY"
replace minfuel_x = 65.1 if year == 2016 & code_wb == "GUY"
replace minfuel_x = 53 if year == 2015 & code_wb == "GUY"
replace minfuel_x = 52 if year == 2014 & code_wb == "GUY"
replace minfuel_x = 58 if year == 2013 & code_wb == "GUY"
replace minfuel_x = 62 if year == 2012 & code_wb == "GUY"
replace minfuel_x = 56.4 if  year == 2011 & code_wb == "GUY"
replace minfuel_x = 52.4 if year == 2010 & code_wb == "GUY"
replace minfuel_x = 47.4 if year == 2009 & code_wb == "GUY"
replace minfuel_x = 43.9 if year == 2008 & code_wb == "GUY"

*-------------------------------------------------------------------------------
* Hong Kong

* Problem: Exports are probably accounting for re-exports and jewelry
* Notes: We take 2000 as last year of data and make it 2020.

replace minfuel_x = . if year > 1999 & code_wb == "HKG"
replace minfuel_x = 2.339556 if year == 2020 & code_wb == "HKG"

*-------------------------------------------------------------------------------
* Laos
* Minor fix in wrong data before 1980's 

replace minfuel_x = . if year < 1974 & code_wb == "LAO"

*-------------------------------------------------------------------------------
* Lebanon - Source: ITC
* Remove the re-exports of diamonds and gold
* Notes: Use 2017 as 2020 and 2001 as 2000

replace minfuel_x = 1.7 if year == 2020 & code_wb == "LBN"
replace minfuel_x = . if year == 2018 & code_wb == "LBN"
replace minfuel_x = . if year == 2017 & code_wb == "LBN"
replace minfuel_x = 0.6 if year == 2016 & code_wb == "LBN"
replace minfuel_x = 0.9 if year == 2015 & code_wb == "LBN"
replace minfuel_x = 1.2 if year == 2014 & code_wb == "LBN"
replace minfuel_x = 8.5 if year == 2013 & code_wb == "LBN"
replace minfuel_x = 2.8 if year == 2012 & code_wb == "LBN"
replace minfuel_x = 0.8 if year == 2011 & code_wb == "LBN"
replace minfuel_x = 1.3 if year == 2010 & code_wb == "LBN"
replace minfuel_x = 2.6 if year == 2009 & code_wb == "LBN"
replace minfuel_x = 3.9 if year == 2008 & code_wb == "LBN"
replace minfuel_x = 3.2 if year == 2007 & code_wb == "LBN"
replace minfuel_x = 3.7 if year == 2006 & code_wb == "LBN"
replace minfuel_x = 6.7 if year == 2005 & code_wb == "LBN"
replace minfuel_x = 6.2 if year == 2004 & code_wb == "LBN"
replace minfuel_x = 4.2 if year == 2003 & code_wb == "LBN"
replace minfuel_x = 3.9 if year == 2002 & code_wb == "LBN"
replace minfuel_x = . if year == 2001 & code_wb == "LBN"
replace minfuel_x = 3.6 if year == 2000 & code_wb == "LBN"

*-------------------------------------------------------------------------------
* Lesotho
* Take 2014 as last year of data from WB (Use 2015 from USGS), the posterior drop in WDI WB data seems odd

replace minfuel_x = . if year > 2014 & code_wb == "LSO"
replace minfuel_x = 34 if year == 2020 & code_wb == "LSO"

*-------------------------------------------------------------------------------
* Macau - World Bank data is probably accounting for re-exports after 2002.
* We take 2002 as last year of data and make it 2020.

replace minfuel_x = . if year > 2001 & code_wb == "MAC"
replace minfuel_x = .005686 if year == 2020 & code_wb == "MAC"

*-------------------------------------------------------------------------------
* Mali - Source: USGS (2012-2015) complemented with ITC
* World Bank estimates are too low, don't seem to include gold

replace minfuel_x = 73 if year == 2020 & code_wb == "MLI"
replace minfuel_x = 74 if year == 2018 & code_wb == "MLI"
replace minfuel_x = 65.9 if year == 2017 & code_wb == "MLI"
replace minfuel_x = 72 if year == 2016 & code_wb == "MLI"
replace minfuel_x = 67 if year == 2015 & code_wb == "MLI"
replace minfuel_x = 67 if year == 2014 & code_wb == "MLI"
replace minfuel_x = 68 if year == 2013 & code_wb == "MLI"
replace minfuel_x = 80 if year == 2012 & code_wb == "MLI"
replace minfuel_x = 71.2 if year == 2011 & code_wb == "MLI"
replace minfuel_x = 80 if year == 2010 & code_wb == "MLI"
replace minfuel_x = . if year == 2009 & code_wb == "MLI"
replace minfuel_x = 76 if year == 2008 & code_wb == "MLI"

*-------------------------------------------------------------------------------
* Malaysia - Source: Oil - USGS (2008-2015) complemented with World Bank
*                    Ores: World Bank	

replace minfuel_x = 18.3 if year == 2020 & code_wb == "MYS"
replace minfuel_x = 19.8 if year == 2018 & code_wb == "MYS"
replace minfuel_x = 19.4 if year == 2017 & code_wb == "MYS"
replace minfuel_x = 17.9 if year == 2016 & code_wb == "MYS"
replace minfuel_x = 13+3.9 if year == 2015 & code_wb == "MYS"
replace minfuel_x = 22+2.9 if year == 2014 & code_wb == "MYS"
replace minfuel_x = 20+3.3 if year == 2013 & code_wb == "MYS"
replace minfuel_x = 12.9+2.3 if year == 2012 & code_wb == "MYS"
replace minfuel_x = 12+2.5 if year == 2011 & code_wb == "MYS"
replace minfuel_x = 10+1.9 if year == 2010 & code_wb == "MYS"
replace minfuel_x = 9.8+1.5 if year == 2009 & code_wb == "MYS"
replace minfuel_x = 12.6+1.8 if year == 2008 & code_wb == "MYS"

*-------------------------------------------------------------------------------
* Namibia - Source: USGS (2012) complemented with ITC
* Notes: Incomplete data for mining from the WDI dataset

replace minfuel_x = 67.3 if year == 2020 & code_wb == "NAM"
replace minfuel_x = 62.1 if year == 2018 & code_wb == "NAM"
replace minfuel_x = 64.4 if year == 2017 & code_wb == "NAM"
replace minfuel_x = 64.3 if year == 2016 & code_wb == "NAM"
replace minfuel_x = 63.5 if year == 2015 & code_wb == "NAM"
replace minfuel_x = 51.2 if year == 2014 & code_wb == "NAM"
replace minfuel_x = 48 if year == 2013 & code_wb == "NAM"
replace minfuel_x = 56.1 if year == 2012 & code_wb == "NAM"
replace minfuel_x = 52 if year == 2011 & code_wb == "NAM"
replace minfuel_x = 50.6 if year == 2010 & code_wb == "NAM"
replace minfuel_x = 41.9 if year == 2009 & code_wb == "NAM"
replace minfuel_x = 49.1 if year == 2008 & code_wb == "NAM"

*-------------------------------------------------------------------------------
* Niger 
* Notes: Take 2016 as last year of data and make it 2020, to avoid weird drop in Uraniun exports seen in the data

replace minfuel_x = . if year > 2016 & code_wb == "NER"
replace minfuel_x = 48.72714 if year == 2020 & code_wb == "NER"
replace minfuel_x = . if year == 2016 & code_wb == "NER"

*-------------------------------------------------------------------------------
* Nicaragua - Source: USGS (2015) - Take 2015 as 2020 and drop the rest of 2010s 
* Notes: Bad data from WB

replace minfuel_x = . if year > 2010 & code_wb == "NIC"
replace minfuel_x = 13.6 if year == 2020 & code_wb == "NIC"

*-------------------------------------------------------------------------------
* Panama - Source: ITC - Take 2015 as 2020 and drop the rest of 2010s 
* Notes: - Bad data from WB
*        - The only gold mine in Panama closed in 2015, but World Bank data doesn't report values of exports before that.

replace minfuel_x = . if year > 2010 & code_wb == "PAN"
replace minfuel_x = 1.51 if year == 2020 & code_wb == "PAN"

*-------------------------------------------------------------------------------
* Peru - Source:  USGS (2007-2015) - Complemented with ITC
* Problem: Bad data from WB
* Notes: Used 2019 as 2018  

replace minfuel_x = 66 if year == 2020 & code_wb == "PER"
replace minfuel_x = 68.5 if year == 2018 & code_wb == "PER"
replace minfuel_x = 69.4 if year == 2017 & code_wb == "PER"
replace minfuel_x = 66.5 if year == 2016 & code_wb == "PER"
replace minfuel_x = 62.3 if year == 2015 & code_wb == "PER"
replace minfuel_x = 63.6 if year == 2014 & code_wb == "PER"
replace minfuel_x = 68.6 if year == 2013 & code_wb == "PER"
replace minfuel_x = 69.2 if year == 2012 & code_wb == "PER"
replace minfuel_x = 69.3 if year == 2011 & code_wb == "PER"
replace minfuel_x = 70.1 if year == 2010 & code_wb == "PER"
replace minfuel_x = 68.1 if year == 2009 & code_wb == "PER"
replace minfuel_x = . if year == 2008 & code_wb == "PER"
replace minfuel_x = 70 if year == 2007 & code_wb == "PER"

*-------------------------------------------------------------------------------
* Saudi Arabia
* Problem: Bad WDI Beta data for last three years of series.
* We take 2016 as the last available year and make it 2020.

replace minfuel_x = . if year > 2015 & code_wb == "SAU"
replace minfuel_x = 78.97028 if year == 2020 & code_wb == "SAU"


*-------------------------------------------------------------------------------
* Sierra Leone - Source: USGS
* Notes: Used 2004 as 2020

replace minfuel_x = 76.9 if year == 2020 & code_wb == "SLE"
replace minfuel_x = . if year == 2018 & code_wb == "SLE"
replace minfuel_x = . if year == 2017 & code_wb == "SLE"
replace minfuel_x = . if year == 2016 & code_wb == "SLE"
replace minfuel_x = . if year == 2015 & code_wb == "SLE"
replace minfuel_x = . if year == 2014 & code_wb == "SLE"
replace minfuel_x = 93.4 if year == 2013 & code_wb == "SLE"
replace minfuel_x = 71 if year == 2012 & code_wb == "SLE"
replace minfuel_x = 90 if year == 2011 & code_wb == "SLE"
replace minfuel_x = . if year == 2010 & code_wb == "SLE"
replace minfuel_x = 90 if year == 2009 & code_wb == "SLE"
replace minfuel_x = 79 if year == 2008 & code_wb == "SLE"
replace minfuel_x = . if year == 2007 & code_wb == "SLE"
replace minfuel_x = . if year == 2006 & code_wb == "SLE"
replace minfuel_x = 90 if year == 2005 & code_wb == "SLE"
replace minfuel_x = . if year == 2004 & code_wb == "SLE"
replace minfuel_x = 92 if year == 2003 & code_wb == "SLE"
replace minfuel_x = . if year == 2002 & code_wb == "SLE"
replace minfuel_x = . if year == 2001 & code_wb == "SLE"
replace minfuel_x = . if year == 2000 & code_wb == "SLE"
replace minfuel_x = . if year == 1999 & code_wb == "SLE"
replace minfuel_x = . if year == 1998 & code_wb == "SLE"
replace minfuel_x = 85 if year == 1997 & code_wb == "SLE"

*-------------------------------------------------------------------------------
* Suriname - Source: USGS
* Problems: WB data doesn't consider gold
* Notes: Take 2008 = 2009. Take 2015=2020

replace minfuel_x = 79.5 if year == 2020 & code_wb == "SUR"
replace minfuel_x = . if year == 2019 & code_wb == "SUR"
replace minfuel_x = . if year == 2018 & code_wb == "SUR"
replace minfuel_x = . if year == 2017 & code_wb == "SUR"
replace minfuel_x = . if year == 2016 & code_wb == "SUR"
replace minfuel_x = . if year == 2015 & code_wb == "SUR"
replace minfuel_x = 86 if year == 2014 & code_wb == "SUR"
replace minfuel_x = 76.5 if year == 2013 & code_wb == "SUR"
replace minfuel_x = 95 if year == 2012 & code_wb == "SUR"
replace minfuel_x = 85 if year == 2011 & code_wb == "SUR"
replace minfuel_x = 85 if year == 2010 & code_wb == "SUR"
replace minfuel_x = 85 if year == 2009 & code_wb == "SUR"
replace minfuel_x = 85 if year == 2008 & code_wb == "SUR"

*-------------------------------------------------------------------------------
* Togo - Source: ITC
* Problem: We take gold and oil exports as re-exports, consider only cement.
* Notes: Take 2001 as 2000 and 2019 as 2020

replace minfuel_x = 17.1 if year == 2020 & code_wb == "TGO"
replace minfuel_x = 18.6 if year == 2018 & code_wb == "TGO"
replace minfuel_x = 19.2 if year == 2017 & code_wb == "TGO"
replace minfuel_x = 21.8 if year == 2016 & code_wb == "TGO"
replace minfuel_x = 18.9 if year == 2015 & code_wb == "TGO"
replace minfuel_x = 24.1 if year == 2014 & code_wb == "TGO"
replace minfuel_x = 24.3 if year == 2013 & code_wb == "TGO"
replace minfuel_x = 25.4 if year == 2012 & code_wb == "TGO"
replace minfuel_x = 28.2 if year == 2011 & code_wb == "TGO"
replace minfuel_x = 29.1 if year == 2010 & code_wb == "TGO"
replace minfuel_x = 33.9 if year == 2009 & code_wb == "TGO"
replace minfuel_x = 29.8 if year == 2008 & code_wb == "TGO"
replace minfuel_x = 55 if year == 2007 & code_wb == "TGO"
replace minfuel_x = . if year == 2006 & code_wb == "TGO"
replace minfuel_x = 37.4 if year == 2005 & code_wb == "TGO"
replace minfuel_x = 37.6 if year == 2004 & code_wb == "TGO"
replace minfuel_x = 32.3 if year == 2003 & code_wb == "TGO"
replace minfuel_x = 43.1 if year == 2002 & code_wb == "TGO"
replace minfuel_x = . if year == 2001 & code_wb == "TGO"
replace minfuel_x = 49.8 if year == 2000 & code_wb == "TGO"

*-------------------------------------------------------------------------------
* Tanzania - Source: USGS
* Problem: World Bank latest data doesn't seem to include gold
* Notes: We use 2015 as 2020

replace minfuel_x = 23.7 if year == 2020 & code_wb == "TZA"
replace minfuel_x = . if year == 2018 & code_wb == "TZA"
replace minfuel_x = . if year == 2017 & code_wb == "TZA"
replace minfuel_x = . if year == 2016 & code_wb == "TZA"
replace minfuel_x = . if year == 2015 & code_wb == "TZA"
replace minfuel_x = 26.1 if year == 2014 & code_wb == "TZA"
replace minfuel_x = 34.8 if year == 2013 & code_wb == "TZA"
replace minfuel_x = 37.6 if year == 2012 & code_wb == "TZA"
replace minfuel_x = 52.5 if year == 2011 & code_wb == "TZA"
replace minfuel_x = 41 if year == 2010 & code_wb == "TZA"
replace minfuel_x = 42 if year == 2009 & code_wb == "TZA"
replace minfuel_x = 37 if year == 2008 & code_wb == "TZA"
replace minfuel_x = 43 if year == 2007 & code_wb == "TZA"
replace minfuel_x = 47 if year == 2006 & code_wb == "TZA"
replace minfuel_x = 45 if year == 2005 & code_wb == "TZA"
replace minfuel_x = 52 if year == 2004 & code_wb == "TZA"
replace minfuel_x = 48 if year == 2003 & code_wb == "TZA"
replace minfuel_x = 42.5 if year == 2002 & code_wb == "TZA"
replace minfuel_x = 40 if year == 2001 & code_wb == "TZA"
replace minfuel_x = 27 if year == 2000 & code_wb == "TZA"
replace minfuel_x = 13.5 if year == 1999 & code_wb == "TZA"
replace minfuel_x = 3.5 if year == 1998 & code_wb == "TZA"

*-------------------------------------------------------------------------------
* Uganda - Source: USGS (2010-2015). Complemented with ITC 
* Problem: World Bank data could be including oil re-exports.
* Notes: We use ITC data excluding gold and fuels, as, according to USGS, these were mostly re-exports. We use 2019 as 2020 and 2001 as 2000.

replace minfuel_x = 1.8 if year == 2020 & code_wb == "UGA"
replace minfuel_x = 2 if year == 2018 & code_wb == "UGA"
replace minfuel_x = 1.7 if year == 2017 & code_wb == "UGA"
replace minfuel_x = 2.8 if year == 2016 & code_wb == "UGA"
replace minfuel_x = 3.5 if year == 2015 & code_wb == "UGA"
replace minfuel_x = 3.9 if year == 2014 & code_wb == "UGA"
replace minfuel_x = 4.7 if year == 2013 & code_wb == "UGA"
replace minfuel_x = 5.1 if year == 2012 & code_wb == "UGA"
replace minfuel_x = . if year == 2011 & code_wb == "UGA"
replace minfuel_x = 5.5 if year == 2010 & code_wb == "UGA"
replace minfuel_x = 6.15 if year == 2009 & code_wb == "UGA"
replace minfuel_x = 5.77 if year == 2008 & code_wb == "UGA"
replace minfuel_x = 2.88 if year == 2007 & code_wb == "UGA"
replace minfuel_x = 2.59 if year == 2006 & code_wb == "UGA"
replace minfuel_x = 2.44 if year == 2005 & code_wb == "UGA"
replace minfuel_x = 0.5 if year == 2004 & code_wb == "UGA"
replace minfuel_x = 0.5 if year == 2003 & code_wb == "UGA"
replace minfuel_x = 1.75 if year == 2002 & code_wb == "UGA"
replace minfuel_x = . if year == 2001 & code_wb == "UGA"
replace minfuel_x = 2.98 if year == 2000 & code_wb == "UGA"

*-------------------------------------------------------------------------------
* Yemen - Source: ITC Mirror dataset
* Notes: Because of civil conflict report of oil exports is poor. We used mirror data from ITC to fix this. We take 2019 as 2020
* For ITC data we ignore gold production, as it was insignificant before the war started.

replace minfuel_x = 67.5 if year == 2020 & code_wb == "YEM"
replace minfuel_x = 57.8 if year == 2018 & code_wb == "YEM"
replace minfuel_x = 40 if year == 2017 & code_wb == "YEM"

*-------------------------------------------------------------------------------
* South Africa - Source: USGS
* Problem: World Bank latest data could be omitting gold
* Notes: We use 2015 as 2020

replace minfuel_x = 40 if year == 2020 & code_wb == "ZAF"
replace minfuel_x = . if year == 2018 & code_wb == "ZAF"
replace minfuel_x = . if year == 2017 & code_wb == "ZAF"
replace minfuel_x = . if year == 2016 & code_wb == "ZAF"
replace minfuel_x = . if year == 2015 & code_wb == "ZAF"
replace minfuel_x = . if year == 2014 & code_wb == "ZAF"
replace minfuel_x = 37 if year == 2013 & code_wb == "ZAF"
replace minfuel_x = 42 if year == 2012 & code_wb == "ZAF"
replace minfuel_x = 45 if year == 2011 & code_wb == "ZAF"
replace minfuel_x = 48 if year == 2010 & code_wb == "ZAF"
replace minfuel_x = 38 if year == 2009 & code_wb == "ZAF"
replace minfuel_x = 41 if year == 2008 & code_wb == "ZAF"

*-------------------------------------------------------------------------------
* Zimbabwe - Source: USGS (). 
* Problem: World Bank could be missing gold and diamond exports
* Notes: 1999 and 1998 values are equal to 2000 to avoid bad data to influence moving average calculations. We use 2016 as 2020

replace minfuel_x = 55 if year == 2020 & code_wb == "ZWE"
replace minfuel_x = . if year == 2018 & code_wb == "ZWE"
replace minfuel_x = . if year == 2017 & code_wb == "ZWE"
replace minfuel_x = . if year == 2016 & code_wb == "ZWE"
replace minfuel_x = 56 if year == 2015 & code_wb == "ZWE"
replace minfuel_x = . if year == 2014 & code_wb == "ZWE"
replace minfuel_x = . if year == 2013 & code_wb == "ZWE"
replace minfuel_x = . if year == 2012 & code_wb == "ZWE"
replace minfuel_x = 45 if year == 2011 & code_wb == "ZWE"
replace minfuel_x = . if year == 2010 & code_wb == "ZWE"
replace minfuel_x = 38 if year == 2009 & code_wb == "ZWE"
replace minfuel_x = . if year == 2008 & code_wb == "ZWE"
replace minfuel_x = . if year == 2007 & code_wb == "ZWE"
replace minfuel_x = . if year == 2006 & code_wb == "ZWE"
replace minfuel_x = . if year == 2005 & code_wb == "ZWE"
replace minfuel_x = 35 if year == 2004 & code_wb == "ZWE"
replace minfuel_x = 40 if year == 2003 & code_wb == "ZWE"
replace minfuel_x = 29 if year == 2002 & code_wb == "ZWE"
replace minfuel_x = 28 if year == 2001 & code_wb == "ZWE"
replace minfuel_x = 29 if year == 2000 & code_wb == "ZWE"
replace minfuel_x = 29 if year == 1999 & code_wb == "ZWE"
replace minfuel_x = 29 if year == 1998 & code_wb == "ZWE"

*-------------------------------------------------------------------------------
* Fiji - Source: USGS. 
* Problem: WB data probably doesn't consider gold exports.
* Notes: We only consider gold exports. Equalized 2000 to 1999 and 1998 to avoid bad data interfering with moving average calculations.
*        Take 2015 as 2020

replace minfuel_x = 8.3 if year == 2020 & code_wb == "FJI"
replace minfuel_x = . if year == 2018 & code_wb == "FJI"
replace minfuel_x = . if year == 2017 & code_wb == "FJI"
replace minfuel_x = . if year == 2016 & code_wb == "FJI"
replace minfuel_x = . if year == 2015 & code_wb == "FJI"
replace minfuel_x = 8.2 if year == 2014 & code_wb == "FJI"
replace minfuel_x = 9.9 if year == 2013 & code_wb == "FJI"
replace minfuel_x = . if year == 2012 & code_wb == "FJI"
replace minfuel_x = . if year == 2011 & code_wb == "FJI"
replace minfuel_x = . if year == 2010 & code_wb == "FJI"
replace minfuel_x = . if year == 2009 & code_wb == "FJI"
replace minfuel_x = . if year == 2008 & code_wb == "FJI"
replace minfuel_x = 0.3 if year == 2007 & code_wb == "FJI"
replace minfuel_x = 5.2 if year == 2006 & code_wb == "FJI"
replace minfuel_x = . if year == 2005 & code_wb == "FJI"
replace minfuel_x = 6.5 if year == 2004 & code_wb == "FJI"
replace minfuel_x = . if year == 2003 & code_wb == "FJI"
replace minfuel_x = . if year == 2002 & code_wb == "FJI"
replace minfuel_x = 6 if year == 2001 & code_wb == "FJI"
replace minfuel_x = 6 if year == 2000 & code_wb == "FJI"
replace minfuel_x = 6 if year == 1999 & code_wb == "FJI"
replace minfuel_x = 6 if year == 1998 & code_wb == "FJI"

*-------------------------------------------------------------------------------
* Paraguay - Source: ITC
* WB seems to be considering electricity exports
* Paraguay doesn't seen to have any relevant mineral exporting industry, we assign 0 and replicate 2001 until 1998 to avoid bad data affecting moving average calculations.

replace minfuel_x = 0 if year == 2020 & code_wb == "PRY"
replace minfuel_x = 0 if year == 2018 & code_wb == "PRY"
replace minfuel_x = 0.5 if year == 2017 & code_wb == "PRY"
replace minfuel_x = 0.5 if year == 2016 & code_wb == "PRY"
replace minfuel_x = 0.6 if year == 2015 & code_wb == "PRY"
replace minfuel_x = 0.4 if year == 2014 & code_wb == "PRY"
replace minfuel_x = 0.2 if year == 2013 & code_wb == "PRY"
replace minfuel_x = 0.1 if year == 2012 & code_wb == "PRY"
replace minfuel_x = 0 if year == 2011 & code_wb == "PRY"
replace minfuel_x = 0 if year == 2010 & code_wb == "PRY"
replace minfuel_x = 0 if year == 2009 & code_wb == "PRY"
replace minfuel_x = 0 if year == 2008 & code_wb == "PRY"
replace minfuel_x = 0 if year == 2007 & code_wb == "PRY"
replace minfuel_x = 0 if year == 2006 & code_wb == "PRY"
replace minfuel_x = 0 if year == 2005 & code_wb == "PRY"
replace minfuel_x = 0 if year == 2004 & code_wb == "PRY"
replace minfuel_x = 0 if year == 2003 & code_wb == "PRY"
replace minfuel_x = 0.1 if year == 2002 & code_wb == "PRY"
replace minfuel_x = 0.1 if year == 2001 & code_wb == "PRY"
replace minfuel_x = 0.1 if year == 2000 & code_wb == "PRY"
replace minfuel_x = 0.1 if year == 1999 & code_wb == "PRY"
replace minfuel_x = 0.1 if year == 1998 & code_wb == "PRY"

*-------------------------------------------------------------------------------
* Bahrain - Source: USGS
* WB seems to be considering exports of manufactured metals
* We use 2015 as 2020

replace minfuel_x = 45 if year == 2020 & code_wb == "BHR"
replace minfuel_x = . if year == 2018 & code_wb == "BHR"
replace minfuel_x = . if year == 2017 & code_wb == "BHR"
replace minfuel_x = . if year == 2016 & code_wb == "BHR"
replace minfuel_x = . if year == 2015 & code_wb == "BHR"
replace minfuel_x = 56 if year == 2014 & code_wb == "BHR"
replace minfuel_x = 73.2 if year == 2013 & code_wb == "BHR"
replace minfuel_x = 78.3 if year == 2012 & code_wb == "BHR"
replace minfuel_x = 76.8 if year == 2011 & code_wb == "BHR"
replace minfuel_x = 74.2 if year == 2010 & code_wb == "BHR"
replace minfuel_x = . if year == 2009 & code_wb == "BHR"
replace minfuel_x = 80 if year == 2008 & code_wb == "BHR"
replace minfuel_x = 80.1 if year == 2007 & code_wb == "BHR"
replace minfuel_x = 79.3 if year == 2006 & code_wb == "BHR"
replace minfuel_x = 78 if year == 2005 & code_wb == "BHR"
replace minfuel_x = 73.3 if year == 2004 & code_wb == "BHR"
replace minfuel_x = 71.2 if year == 2003 & code_wb == "BHR"
replace minfuel_x = 68.1 if year == 2002 & code_wb == "BHR"
replace minfuel_x = 67 if year == 2001 & code_wb == "BHR"
replace minfuel_x = 70 if year == 2000 & code_wb == "BHR"
replace minfuel_x = 62 if year == 1999 & code_wb == "BHR"
replace minfuel_x = 60 if year == 1998 & code_wb == "BHR"

*-------------------------------------------------------------------------------
* Cameroon - Source: USGS
* WB differs from USGS estimates
* We use 2014 as 2020 and 2009 replicated to 2008 due to lack of data

replace minfuel_x = 51 if year == 2020 & code_wb == "CMR"
replace minfuel_x = . if year == 2018 & code_wb == "CMR"
replace minfuel_x = . if year == 2017 & code_wb == "CMR"
replace minfuel_x = . if year == 2016 & code_wb == "CMR"
replace minfuel_x = . if year == 2015 & code_wb == "CMR"
replace minfuel_x = . if year == 2014 & code_wb == "CMR"
replace minfuel_x = 52 if year == 2013 & code_wb == "CMR"
replace minfuel_x = . if year == 2012 & code_wb == "CMR"
replace minfuel_x = 50 if year == 2011 & code_wb == "CMR"
replace minfuel_x = 50 if year == 2010 & code_wb == "CMR"
replace minfuel_x = 50 if year == 2009 & code_wb == "CMR"
replace minfuel_x = 50 if year == 2008 & code_wb == "CMR"

*-------------------------------------------------------------------------------
* Jamaica - Source: ITC
* WB considers Alumina, we extract that share here
* We use 2019 as 2020

replace minfuel_x = 24.9 if year == 2020 & code_wb == "JAM"
replace minfuel_x = 20.6 if year == 2018 & code_wb == "JAM"
replace minfuel_x = 25.7 if year == 2017 & code_wb == "JAM"
replace minfuel_x = 23.5 if year == 2016 & code_wb == "JAM"
replace minfuel_x = 27.6 if year == 2015 & code_wb == "JAM"
replace minfuel_x = 32.5 if year == 2014 & code_wb == "JAM"
replace minfuel_x = 31.1 if year == 2013 & code_wb == "JAM"
replace minfuel_x = 30.5 if year == 2012 & code_wb == "JAM"
replace minfuel_x = 31.9 if year == 2011 & code_wb == "JAM"
replace minfuel_x = 31.7 if year == 2010 & code_wb == "JAM"
replace minfuel_x = 23.4 if year == 2009 & code_wb == "JAM"
replace minfuel_x = 22.7 if year == 2008 & code_wb == "JAM"

*-------------------------------------------------------------------------------
* CAR - Source: ITC (2020) & Reuters (2014) (Gold, diamonds fuelling conflict in Central African Republic: U.N. panel)
* We modify the value of 2020
* We use 2019 as 2020

replace minfuel_x = . if year > 2013 & code_wb == "CAF"
replace minfuel_x = 24.24 if year == 2014 & code_wb == "CAF"
replace minfuel_x = 19.1 if year == 2020 & code_wb == "CAF"

********************************************************************************
**# Step 3.1: Finalizing Data: Calculating Moving Averages
********************************************************************************

* First we interpolate to fill missing variables
by code_wb: ipolate minfuel_x year, gen(intfuel)
replace minfuel_x = intfuel if minfuel_x == .
drop intfuel

* Next we replace missing variables at the start of the series by the most recent observation
* This won't affect our estimations because we are only using observations post 2010 from here.
gen year2 = -year
sort code_wb year2
by code_wb: replace minfuel_x = minfuel_x[_n-1] if minfuel_x == .
sort code_wb year
drop year2

* Now we calculate moving averages

* Generating moving averages *

* Sorting
sort code_wb year

* Moving Average (-1/+1)

* Generating Variable ma1
gen minfuel_x_ma1 = 0

* Estimating ma1 for the sample except 1960 and 2020
foreach ma1 of numlist 2/59{
	by code_wb: replace minfuel_x_ma1 = (minfuel_x[`ma1'-1] + minfuel_x[`ma1'] + minfuel_x[`ma1'+1])/3 if _n == `ma1' 
}

* Estimating ma1 for 1960 and 2020
by code_wb: replace minfuel_x_ma1 = (minfuel_x[1] + minfuel_x[2])/2 if _n == 1
by code_wb: replace minfuel_x_ma1 = (minfuel_x[59] + minfuel_x[60])/2 if _n == 60


* Taking moving averages with a window of 2

* Generating Variable ma2
gen minfuel_x_ma2 = 0

foreach ma2 of numlist 3/58{
	by code_wb: replace minfuel_x_ma2 = ///
	(minfuel_x[`ma2'-2] + minfuel_x[`ma2'-1] + minfuel_x[`ma2'] + minfuel_x[`ma2'+1] + minfuel_x[`ma2'+2])/5 ///
	if _n == `ma2' 
}
	
* Estimating ma2 for 1960, 1961, 2018 and 2020
by code_wb: replace minfuel_x_ma2 = (minfuel_x[1] + minfuel_x[2] + minfuel_x[3])/3 if _n == 1
by code_wb: replace minfuel_x_ma2 = (minfuel_x[1] + minfuel_x[2] + minfuel_x[3] + minfuel_x[4])/4 if _n == 2
by code_wb: replace minfuel_x_ma2 = (minfuel_x[57] + minfuel_x[58] + minfuel_x[59] + minfuel_x[60])/4 if _n == 59
by code_wb: replace minfuel_x_ma2 = (minfuel_x[58] + minfuel_x[59] + minfuel_x[60])/3 if _n == 60

********************************************************************************
**# Step 3.1: Finalizing Data: Combining with GJV2016
********************************************************************************

* Keep desired years
keep if year == 1960 | year == 1965 | year == 1970 | year == 1975 | year == 1980 | year == 1985 | year == 1990 | year == 1995 ///
| year == 2000 | year == 2005 | year == 2010 | year == 2015 | year == 2020

* Merge GJV2016 data
rename code_wb ccode
replace ccode = "ZAR" if ccode == "COD"
merge 1:1 ccode year using "raw_datasets/exports_gjv", keepusing(min_sh_x)
sort ccode year
rename ccode code_wb
replace code_wb = "COD" if code_wb == "ZAR"
drop _merge

* Drop unnecesary variables
drop oresmetals_x_b oresmetals_x fuel_x_b fuel_x oresmetals_x_comb fuel_x_comb

* Rename minfuel_x variables and label them
rename minfuel_x min_sh_x_n
rename minfuel_x_ma1 min_sh_x_n_ma1
rename minfuel_x_ma2 min_sh_x_n_ma2
la var min_sh_x_n "Share of minerals on merchandise exports (New)"
la var min_sh_x_n_ma1 "Share of minerals on merchandise exports (New) - Moving Avg. +/- 1"
la var min_sh_x_n_ma2 "Share of minerals on merchandise exports (New) - Moving Avg. +/- 2"

* Generate combined minfuel_x variable (which will incoporate GJV2016 data)
gen min_sh_x_c = min_sh_x
gen min_sh_x_c_ma1 = min_sh_x
gen min_sh_x_c_ma2 = min_sh_x

* Replace data with GJV2016 for observations pre-2010
replace min_sh_x_c = min_sh_x_n if year > 2005
replace min_sh_x_c_ma1 = min_sh_x_n_ma1 if year > 2005
replace min_sh_x_c_ma2 = min_sh_x_n_ma2 if year > 2005
la var min_sh_x_c "Share of minerals on merchandise exports (Combined)"
la var min_sh_x_c_ma1 "Share of minerals on merchandise exports (Combined) - Moving Avg. +/- 1"
la var min_sh_x_c_ma2 "Share of minerals on merchandise exports (Combined) - Moving Avg. +/- 2"

* Finally, we replace GJV2016 values for 2000 and 2005 with the processed WDI
* data for particular countries for which GJV2016 values seem odd considering 
* the mineral industry characeteristics of said countries 

* Honduras
replace min_sh_x_c = min_sh_x_n if year == 2000 & code_wb == "HND"
replace min_sh_x_c = min_sh_x_n if year == 2005 & code_wb == "HND"
replace min_sh_x_c_ma1 = min_sh_x_n_ma1 if year == 2000 & code_wb == "HND"
replace min_sh_x_c_ma1 = min_sh_x_n_ma1 if year == 2005 & code_wb == "HND"
replace min_sh_x_c_ma2 = min_sh_x_n_ma2 if year == 2000 & code_wb == "HND"
replace min_sh_x_c_ma2 = min_sh_x_n_ma2 if year == 2005 & code_wb == "HND"

* Uganda 
replace min_sh_x_c = min_sh_x_n if year == 2000 & code_wb == "UGA"
replace min_sh_x_c = min_sh_x_n if year == 2005 & code_wb == "UGA"
replace min_sh_x_c_ma1 = min_sh_x_n_ma1 if year == 2000 & code_wb == "UGA"
replace min_sh_x_c_ma1 = min_sh_x_n_ma1 if year == 2005 & code_wb == "UGA"
replace min_sh_x_c_ma2 = min_sh_x_n_ma2 if year == 2000 & code_wb == "UGA"
replace min_sh_x_c_ma2 = min_sh_x_n_ma2 if year == 2005 & code_wb == "UGA"

* Bahrain
replace min_sh_x_c = min_sh_x_n if year == 2000 & code_wb == "BHR"
replace min_sh_x_c = min_sh_x_n if year == 2005 & code_wb == "BHR"
replace min_sh_x_c_ma1 = min_sh_x_n_ma1 if year == 2000 & code_wb == "BHR"
replace min_sh_x_c_ma1 = min_sh_x_n_ma1 if year == 2005 & code_wb == "BHR"
replace min_sh_x_c_ma2 = min_sh_x_n_ma2 if year == 2000 & code_wb == "BHR"
replace min_sh_x_c_ma2 = min_sh_x_n_ma2 if year == 2005 & code_wb == "BHR"

* Ethiopia
replace min_sh_x_c = min_sh_x_n if year == 2000 & code_wb == "ETH"
replace min_sh_x_c = min_sh_x_n if year == 2005 & code_wb == "ETH"
replace min_sh_x_c_ma1 = min_sh_x_n_ma1 if year == 2000 & code_wb == "ETH"
replace min_sh_x_c_ma1 = min_sh_x_n_ma1 if year == 2005 & code_wb == "ETH"
replace min_sh_x_c_ma2 = min_sh_x_n_ma2 if year == 2000 & code_wb == "ETH"
replace min_sh_x_c_ma2 = min_sh_x_n_ma2 if year == 2005 & code_wb == "ETH"

* Lebanon
replace min_sh_x_c = min_sh_x_n if year == 2000 & code_wb == "LBN"
replace min_sh_x_c = min_sh_x_n if year == 2005 & code_wb == "LBN"
replace min_sh_x_c_ma1 = min_sh_x_n_ma1 if year == 2000 & code_wb == "LBN"
replace min_sh_x_c_ma1 = min_sh_x_n_ma1 if year == 2005 & code_wb == "LBN"
replace min_sh_x_c_ma2 = min_sh_x_n_ma2 if year == 2000 & code_wb == "LBN"
replace min_sh_x_c_ma2 = min_sh_x_n_ma2 if year == 2005 & code_wb == "LBN"

* Sierra Leone
replace min_sh_x_c = min_sh_x_n if year == 2000 & code_wb == "SLE"
replace min_sh_x_c = min_sh_x_n if year == 2005 & code_wb == "SLE"
replace min_sh_x_c_ma1 = min_sh_x_n_ma1 if year == 2000 & code_wb == "SLE"
replace min_sh_x_c_ma1 = min_sh_x_n_ma1 if year == 2005 & code_wb == "SLE"
replace min_sh_x_c_ma2 = min_sh_x_n_ma2 if year == 2000 & code_wb == "SLE"
replace min_sh_x_c_ma2 = min_sh_x_n_ma2 if year == 2005 & code_wb == "SLE"

* Tanzania
replace min_sh_x_c = min_sh_x_n if year == 2000 & code_wb == "TZA"
replace min_sh_x_c = min_sh_x_n if year == 2005 & code_wb == "TZA"
replace min_sh_x_c_ma1 = min_sh_x_n_ma1 if year == 2000 & code_wb == "TZA"
replace min_sh_x_c_ma1 = min_sh_x_n_ma1 if year == 2005 & code_wb == "TZA"
replace min_sh_x_c_ma2 = min_sh_x_n_ma2 if year == 2000 & code_wb == "TZA"
replace min_sh_x_c_ma2 = min_sh_x_n_ma2 if year == 2005 & code_wb == "TZA"

* Singapore
replace min_sh_x_c = min_sh_x_n if code_wb == "SGP"
replace min_sh_x_c_ma1 = min_sh_x_n_ma1 if code_wb == "SGP"
replace min_sh_x_c_ma2 = min_sh_x_n_ma2 if code_wb == "SGP"

save "processed_datasets/dataset_metfuel", replace
